const BaseHandler = require('../base-handler');
const SystemServices = require('../../../constants').SystemServices;
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;
const moment = require('moment');

class TasksGroupHandler extends BaseHandler {
    get pattern() {
        return "/goods/taskGroup/:type";
    }

    // 获取所有列表数据
    doGET(request, response) {
        if (!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '请登录系统后再调用此接口！'
            });
        } else {
            var type = request.params.type;
            var params = { type, userId: request.session.user.id };
            if (type === 'find') {
                params.data = { page: request.query.page || 1, size: request.query.size || 10 }
            } else {
                params.data = { taskGroupName: request.query.taskGroupName };
            }
            this.Controller.taskGroupOperation(params, (result) => {
                if (result.error) {
                    return response.json({
                        success: false,
                        error: result.error
                    });
                }
                response.json({
                    success: true,
                    result: result
                });
            });

        }
    }
    
    doPOST(request, response) {
        if (!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '请登录系统后再调用此接口！'
            });
        } else {
            var params = { userId: request.session.user.id, channel: '', placeId: '', data: { taskGroupName: request.body.taskGroupName } };
            this.Controller.addTaskGroup(params, (result) => {
                if (result.error) {
                    return response.json({
                        success: false,
                        error: result.error
                    });
                }
                response.json({
                    success: true,
                    result: result
                });
            });
        }
    }


    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);

    }
}

module.exports = TasksGroupHandler;